package com.bestdata.forward.datareceive;

import com.bestdata.forward.cachecenter.CacheCenterFactory;
import com.bestdata.forward.httptool.impl.*;
import com.bestdata.forward.model.DeviceDTO;
import com.bestdata.forward.model.SensorConfigDTO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.DependsOn;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.PostConstruct;
import java.util.Map;

/**
 * @author chizy
 * @description 接收数据解析核心
 * @date 2021年1月17日13:45:33
 * @version 1.0.0
 */
@Service
//@DependsOn("springContextHolder")
public class DataAnalysisCenter extends Thread{

    private Logger logger = LoggerFactory.getLogger(this.getClass());
    private Thread thread;
    private static final boolean stop = false;

    /**
     * 风速风向实现类
     */
    private HttpPostSendAnemoMeterServiceImpl anemoMeterService = new HttpPostSendAnemoMeterServiceImpl();

    /**
     * 腐蚀计实现类
     */
    private HttpPostSendCorrosionMeterServiceImpl corrosionMeterService = new HttpPostSendCorrosionMeterServiceImpl();

    /**
     * 测缝计实现类
     */
    private HttpPostSendCrackMeterServiceImpl crackMeterService = new HttpPostSendCrackMeterServiceImpl();

    /**
     * 拉线位移计实现类
     */
    private HttpPostSendDisMeterServiceImpl disMeterService = new HttpPostSendDisMeterServiceImpl();

    /**
     * 应力计实现类
     */
    private HttpPostSendSurfaceStrainMeterServiceImpl surfaceStrainMeterService = new HttpPostSendSurfaceStrainMeterServiceImpl();

    /**
     * 温度实现类
     */
    private HttpPostSendTemperatureMeterServiceImpl temperatureMeterService = new HttpPostSendTemperatureMeterServiceImpl();

    /**
     * 温湿度实现类
     */
    private HttpPostSendTHMetersServiceImpl thMetersService = new HttpPostSendTHMetersServiceImpl();

    @PostConstruct
    public void start(){
        this.thread = new Thread(this);
        this.thread.start();
    }

    @Override
    public void run(){

        while (!stop){

            DeviceDTO take = new DeviceDTO();
            try {

                take = CacheCenterFactory.queue.take();
            }catch (Exception e){
                logger.error("Data processing exception 》》》》》》》》"+e);
            }
            //todo 已知类型使用传感器测值的有 ：【温湿度、风速风向】。其余皆使用value值,但应变计可能会有温度(传感器测值)

            String devid = take.getDevid();
            //避免设备名称出现乱码，进行强制转换
            String devname = take.getDevname().replace("& #39;","'");
            devname = devname.replace("’","'");

            //pms设备配置信息
            DeviceDTO deviceDTO = CacheCenterFactory.deviceConfigMap.get(devid);

            if(StringUtils.isEmpty(deviceDTO)){

                continue;
            }

            if(take.getSensors().size() > 0 && !StringUtils.isEmpty(deviceDTO.getSensors())){
                for (int i = 0; i < take.getSensors().size(); i++) {
                    for (int j = 0; j < deviceDTO.getSensors().size(); j++) {
                        if(deviceDTO.getSensors().get(j).getSid().equals(take.getSensors().get(i).getSid())){
                            take.getSensors().get(i).setSenType(deviceDTO.getSensors().get(j).getSenType());
                        }
                    }
                }
            }

            String codeName = deviceDTO.getBridgeCodeName();
            take.setBridgeCodeName(codeName);

            Map<String, SensorConfigDTO> stringSensorConfigDTOMap = CacheCenterFactory.bridgeSensorConfigConnectionMap.get(codeName);

            SensorConfigDTO sensorConfigDTO = stringSensorConfigDTOMap.get(devname);

            /*System.out.println("sensorConfigDTO:"+sensorConfigDTO);
            System.out.println("take:"+take);*/

            //联智北斗平台传感器类型
            String sensorCode = sensorConfigDTO.getSensorCode();

            switch (sensorCode){
                //温湿度
                case "THMeter":
                    this.thMetersService.httpSend(sensorConfigDTO, take);
                    break;
                //风速风向
                case "AnemoMeter":
                    this.anemoMeterService.httpSend(sensorConfigDTO, take);
                    break;
                //应变计
                case "SurfaceStrainMeter":
                    this.surfaceStrainMeterService.httpSend(sensorConfigDTO, take);
                    break;
                //拉线式位移计
                case "DisMeter":
                    this.disMeterService.httpSend(sensorConfigDTO, take);
                    break;
                //裂缝计
                case "CrackMeter":
                    this.crackMeterService.httpSend(sensorConfigDTO, take);
                    break;
                //腐蚀剂
                case "CorrosionMeter":
                    this.corrosionMeterService.httpSend(sensorConfigDTO, take);
                    break;
                //温度计
                case "TemperatureMeter":
                    this.temperatureMeterService.httpSend(sensorConfigDTO, take);
                    break;
                //默认方法
                default:
                    break;
            }
        }
    }
}
